package ebedynky.services;

import ebedynky.beans.HibernateUtil;
import ebedynky.beans.OfferItem;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 * @author davidon1
 */
public class OfferItemService {

    /**
     * Seesion - current connection to the db.
     */
    private Session session;

    /**
     * Constructor, retrieces the session (connection).
     */
    public OfferItemService() {
        this.session = HibernateUtil.getSessionFactory().openSession();
    }

    /**
     * Closes the session (connection).
     * @return success/failure.
     */
    public boolean close() {
        this.session.close();
        return !(this.session.isOpen());
    }

    /**
     * Restarts the session (connection).
     * @return success/failure.
     */
    public boolean restart() {
        this.session.close();
        if (this.session.isOpen()) {
            throw new IllegalStateException("Session could not be closed.");
        }
        this.session = HibernateUtil.getSessionFactory().openSession();
        return this.session.isOpen();
    }

    /**
     * Adds offeritem to the db.
     * @param oi OfferItem to be inserted
     * @return ID of the added offeritem
     */
    public int addOfferItem(OfferItem oi) {
        session.beginTransaction();
        int id = getMaxOfferItemID() + 1;
        oi.setOfferItemID(id);
        id = (Integer) session.save(oi);
        session.getTransaction().commit();
        return (id);
    }

    /**
     * Retrieves the maximum id currently in the database of offeritems.
     * @return Max ID
     */
    public int getMaxOfferItemID() {
        String hql = "select max(o.offerItemID) from OfferItem o";
        List list = session.createQuery(hql).list();
        if (list.get(0) != null) {
            Integer maxoiid = (Integer) list.get(0);
            return maxoiid;
        } else {
            return 0;
        }
    }

    /**
     * Retrieves a offeritem identified by the ID.
     * @param id of the OfferItem
     * @return OfferItem with the ID given (null if not found)
     */
    public OfferItem getOfferItemByID(int id) {
        session.beginTransaction();
        OfferItem oi = (OfferItem) session.get(OfferItem.class, new Integer(id));
        session.getTransaction().commit();
        return (oi);
    }

    /**
     * Retrieves a list of all offeritems stored in the db.
     * @return List of all OfferItems (null if no offeritems)
     */
    public List<OfferItem> getAllOfferItems() {
        session.beginTransaction();
        List<OfferItem> oiList = session.createQuery("from OfferItem").list();
        session.getTransaction().commit();
        return (oiList);
    }

    /**
     * Updates a specified offeritem identified by ID.
     * @param newOfferItem an updated offeritem
     */
    public void updateOfferItem(OfferItem newOfferItem) {
        session.beginTransaction();
        OfferItem oi = (OfferItem) session.get(OfferItem.class, new Integer(newOfferItem.getOfferItemID()));
        oi.setBox(newOfferItem.getBox());
        oi.setOffer(newOfferItem.getOffer());
        oi.setOrderitems(newOfferItem.getOrderitems());
        oi.setPieces(newOfferItem.getPieces());
        oi.setPrice(newOfferItem.getPrice());
        oi.setProduct(newOfferItem.getProduct());
        oi.setProfit(newOfferItem.getProfit());
        oi.setVolume(newOfferItem.getVolume());
        oi.setWeight(newOfferItem.getWeight());
        session.flush();
        session.getTransaction().commit();
        session.clear();
    }

    /**
     * Removes the OfferItem from db.
     * @param id OfferItem ID for removal
     * @return true if successful, false otherwise
     */
    public boolean deleteOfferItem(int id) {
        try {
            session.beginTransaction();
            OfferItem oi = (OfferItem) session.get(OfferItem.class, new Integer(id));
            session.delete(oi);
            session.getTransaction().commit();
            return (true);
        } catch (HibernateException ex) {
            return (false);
        }
    }
}
